//
//  WBNewfeatureViewController.swift
//  weibo
//
//  Created by yanghong on 15/12/9.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
let count = 4
class WBNewfeatureViewController: UIViewController, UIScrollViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    func setupUI() {
        //添加到View上
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: view.frame.width*0.5, y: view.frame.height-100, width: 0, height: 0)
        view.addSubview(pageControl)
        // 设置scrollView
        
        
        for var i = 0; i < count; i++ {
            
            let imgView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            
            imgView.frame = CGRect(x: CGFloat(i)*(view.frame.width), y: 0, width: view.frame.width, height: view.frame.height)
            
            scrollView.addSubview(imgView)
            //如果是添加最后一张图片 则把两个按钮给添加上
            if i == (count - 1) {
                
                setupButton(imgView)
                
            }
            
        }
        // 设置scrollView属性
        scrollView.contentSize = CGSize(width: CGFloat(count)*(view.frame.width), height: 0)
        scrollView.bounces = false
        
        
    }
    // 设置最后一张图片上的按钮
    
    private func setupButton(imageView: UIImageView) {
        
        imageView.userInteractionEnabled = true
        //设置frame
        enterButton.center = CGPoint(x: imageView.frame.size.width*0.5, y: imageView.frame.height-150)
        sharedButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame)-30)
        imageView.addSubview(sharedButton)
        imageView.addSubview(enterButton)
        
    }
    // MARK -  UIScrollView delegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let currentPageNumber =  scrollView.contentOffset.x / view.frame.width + 0.5
        
        pageControl.currentPage = Int(currentPageNumber)
        
        
    }
    // MARK -  enterButton 点击事件
    
    @objc  private func enterButtonClciked() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
        print("进入微博")
    }
    
    // MARK -  sharedButton 点击事件
    
    @objc   private func sharedButtonClicked(button: UIButton) {
        
        button.selected = !button.selected
        
    }
    
    // MARK -  懒加载控件
    lazy var scrollView: UIScrollView = {
        
        let scView = UIScrollView()
        
        scView.delegate = self
        
        scView.pagingEnabled = true
        
        scView.showsHorizontalScrollIndicator = false
        
        scView.backgroundColor = UIColor.orangeColor()
        
        return scView
    }()
    // MARK -  懒加载pageControl
    lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.numberOfPages = count
        pageControl.currentPageIndicatorTintColor = UIColor.darkGrayColor()
        pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
        return pageControl
    }()
    // MARK -  enterButton
    lazy var enterButton: UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "enterButtonClciked", forControlEvents: .TouchUpInside)
        
        button.setTitle("进入微博", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        button.sizeToFit()
        
        return button
    }()
    
    // MARK -  sharedButton
    
    lazy var sharedButton: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "sharedButtonClicked:", forControlEvents: .TouchUpInside)
        
        button.setTitle(" 分享微博", forState: .Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        
        button.sizeToFit()
        
        return button
    }()
    
}
